define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Productmanager_brand_edit, GoPicture, GoDialog, Auth,GoCatTypeAuth,GoPage,$timeout,$stateParams) {

        $scope.scope = new Object();
        $scope.scope.id = $stateParams.id;
        $scope.scope.name = "";
        $scope.scope.url="";
        $scope.scope.explain="";
        $scope.input = new Object();
        $scope.cats = new Array(); //作为显示
        $scope.input.cat_type = new Array();//作为提交
		$scope.logo = new Object();//logo图片
        $scope.aptitude = new Object();//资质图片
        
		//初始化页面
		$scope.init_data = function(){
			if(!$scope.scope.id) return;
			Productmanager_brand_edit.detail($scope.scope.id,function(data){
				var detail = data.result;
				console.log(detail)
				$scope.scope = detail;
				for (var i=0;i<detail.goods_cat.length;i++) {
					$scope.cats.push(detail.goods_cat[i].cat_name);
        			$scope.input.cat_type.push(detail.goods_cat[i].cat_name+":"+detail.goods_cat[i].cat_id);//格式不能错
				}
				$scope.aptitude = detail.aptitude;//资质
				$scope.logo = detail.logo; //logo
			})
		}
		$scope.init_data();

		
    /************分类三级联动*****************/
        $scope.initialize = function(){//后面要初始化这3个参数，所以把他们放在函数里
            $scope.input.cat1 = {id:0,name:"请选择大分类"};
            $scope.input.cat2 = {id:0,name:"请选择中分类"};
            $scope.input.cat3 = {id:0,name:"请选择小分类"};
        }
        $scope.initialize();
        //下拉侦听
        $scope.listen_cat1 = function(data, obj) {
            $scope.input.cat2 = {id:0,name:"请选择中分类"};
            $scope.input.cat3 = {id:0,name:"请选择小分类"};
        };
        $scope.listen_cat2 = function(data, obj) {
            $scope.input.cat3 = {id:0,name:"请选择小分类"};
        };
        //定义绑定
        var items_cat = new Array();
        var obj1_cat = { name: 'input.cat1' };
        var obj2_cat = { name: 'input.cat2' };
        var obj3_cat = { name: 'input.cat3' };
        items_cat.push(obj1_cat);
        items_cat.push(obj2_cat);
        items_cat.push(obj3_cat);
        GoCatTypeAuth.init_load_only($scope, items_cat);
    /************分类三级联动*****************/
    $scope.doSelectCat1 = function(){
        //id是值 name是显示
        GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
            $scope.input.cat1 = data;
        });
    }
    $scope.doSelectCat2 = function(){
        //id是值 name是显示
        GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
            $scope.input.cat2 = data;
        });
    }
    $scope.doSelectCat3 = function(){
        //id是值 name是显示
        GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
            $scope.input.cat3 = data;
        });
    }
    /*-------品类处理---------*/
      	//添加分类
      	$scope.add_type = function(){
      		$scope.cat_id = "";
			$scope.cat_name = "";
      		if($scope.input.cat3){
        		$scope.cat_name=$scope.input.cat1.name+"/"+$scope.input.cat2.name+"/"+$scope.input.cat3.name;
        		$scope.cat_id=$scope.cat_name+":"+$scope.input.cat3.id;
        	}else if($scope.input.cat2){
        		$scope.cat_name=$scope.input.cat1.name+"/"+$scope.input.cat2.name;
        		$scope.cat_id=$scope.cat_name+":"+$scope.input.cat2.id;
        	}else if($scope.input.cat1){
        		$scope.cat_name=$scope.input.cat1.name;
        		$scope.cat_id=$scope.cat_name+":"+$scope.input.cat1.id;
        	}/*else{
        		GoDialog.toast("请至少选择一级品类！");
        	}*/
        	if($scope.cat_id&&$scope.cat_name){
        		for (var i=0;i<$scope.input.cat_type.length;i++) {
        			if($scope.input.cat_type[i]==$scope.cat_id){
        				GoDialog.toast("不能重复添加品类！");
        				return;
        			}
        		}
        		$scope.cats.push($scope.cat_name);//显示
        		$scope.input.cat_type.push($scope.cat_id);
        		$scope.initialize();//初始化分类3个modal
        		$scope.showSelectCat();//添加完，关闭弹窗
        	}
      	}
        //删除。旗舰店 品牌编辑时，如果是审核通过的品牌，品类只能添加新的，不能删除		
        $scope.delete_type = function(val) {
            $scope.cats.splice(val, 1);
            $scope.input.cat_type.splice(val, 1);
        };
        //控制下拉框的显示
        $scope.showSelectCat = function(){
        	$scope.isShowSelectCat = !$scope.isShowSelectCat;
        }
	/*----------品类处理---------*/
		
		//提交表单
        $scope.submit = function(){
            $scope.add_type();
            if(!$scope.scope.name){
                GoDialog.toast("请输入品牌名称！");
            }else if($scope.input.cat_type.length== 0){
                GoDialog.toast("请选择品类！");
            }else{
                Productmanager_brand_edit.edit_brand($scope.scope.name,$scope.scope.id,$scope.scope.url,$scope.scope.explain,$scope.input.cat_type,$scope.logo,$scope.aptitude,function(data){
                    if(data.success == true){
                        GoDialog.toast("保存成功！");
                        GoPage.go_states("productmanager_brand");
                    }else{
                        GoDialog.toast(data.msg);
                    }
                });
            }
        }
        

        /*==============图片处理================*/
        $scope.addStoreImg = function(name) {
            if(name == 'logo'){
            	GoPicture.upload(function(res) {
	                if(res.success) {
	                    $scope.logo.image =res.result.image_src ; //图片 显示用
	             		$scope.logo.id = res.result.image_id; //图片id
	                }else{
	                	GoDialog.toast(res.msg);
	                }
            	});
            }else if(name == 'aptitude'){
            	GoPicture.upload(function(res) {
	                if(res.success) {
	                    $scope.aptitude.image =res.result.image_src ; //图片 显示用
	             		$scope.aptitude.id = res.result.image_id; //图片id
	                }else{
	                	GoDialog.toast(res.msg);
	                }
            	});
            }
        }
        
        //删除图片，通过name来区分资质和logo
        $scope.openFileActSheet = function(name) {
            var buttons = new Array();
            buttons.push({ text: '删除图片', style: '' });
            buttons.push({ text: '显示大图', style: '' });

            GoDialog.sheet('图片操作', '取消', buttons, function(bindex) {
                if(bindex == 0) {//删除图片
                    GoDialog.confirm('删除图片', '是否要删除该图片？', function() {
                        if(name == 'logo'){
                        	$scope.logo = new Object();
                        }else if(name == 'aptitude'){
                        	$scope.aptitude = new Object();
                        }
                    })
                }else if(bindex==1){//显示大图
                	if(name == 'logo'){
                    	GoDialog.showBigImg($scope.logo.image);
                    }else if(name == 'aptitude'){
                    	GoDialog.showBigImg($scope.aptitude.image);
                    }
                }
            });
        }
        
        
    }

    controller.$inject = ['$scope', 'Productmanager_brand_edit', 'GoPicture', 'GoDialog', 'Auth','GoCatTypeAuth','GoPage','$timeout','$stateParams'];

    return app.registerController("productmanager_brand_editctrl",controller);
});